using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;

public class MapLevelUI : MonoBehaviour
{
    public GameObject mapListGo;
    public GameObject levelListGo;
    
    public List<MapUI> mapUIList = new List<MapUI>();

    public GameObject levelTemplatePrefab;//关卡模板预制体
    public GameObject levelGridGo;//关卡列表父物体
    
    //显示地图列表
    public void ShowMapList(MapSO[] mapArray)
    {
        mapListGo.SetActive(true);
        levelListGo.SetActive(false);
        UpdateMapUIList(mapArray);
    }

    private void UpdateMapUIList(MapSO[] mapArray)
    {
        for (var i = 0; i < mapArray.Length; i++)
        {
            mapUIList[i].Show(mapArray[i].startNumnberOfMap, this, i + 1); //显示关卡
        }
    }

    public void OnMapButtonClick(int mapID)
    {
        LevelSelectManager.Instance.SerSelectedMap(mapID);//切换地图
        Debug.Log(mapID);
        ShowLevelGrid();//显示关卡列表
    }

    public void ShowLevelGrid()
    {
        mapListGo.SetActive(false);//隐藏地图列表
        levelListGo.SetActive(true);//显示关卡列表

        int[] starNumberOfLevel = LevelSelectManager.Instance.GetSelectedMap();
        
        //清空关卡列表
        foreach (Transform child in levelGridGo.transform)
        {
            Destroy(child.gameObject);
        }
        
        for (var i = 0; i < starNumberOfLevel.Length; i++)
        {
            GameObject go = Instantiate(levelTemplatePrefab);//实例化模板
            go.GetComponent<RectTransform>().SetParent(levelGridGo.transform);//设置父物体
            go.GetComponent<LevelUI>().Show(starNumberOfLevel[i], i + 1, this); //显示关卡有多少个星星
        }
    }

    public void OnLevelButtonClick(int levelID)
    {
        LevelSelectManager.Instance.SetSelectedLevel(levelID);
    }

    public void OnReturnButtonClick()
    {
        mapListGo.SetActive(true);
        levelListGo.SetActive(false);
    }
}
